<template>
  <div class="app-container">
    <el-form ref="queryRef" :model="queryParams" :inline="true" label-width="80">
      <el-form-item label="订单编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item v-if="!isBelongMatch" label="活动名称" prop="activeName">
        <el-input
          v-model="queryParams.activeName"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="选择订单状态" clearable @change="handleQuery">
          <el-option label="付款中" value="0" />
          <el-option label="付款成功" value="1" />
          <el-option label="取消付款" value="2" />
          <el-option label="退订" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="下单时间" prop="handTime">
        <el-date-picker
          v-model="queryParams.handTime"
          clearable
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="warning" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="orderList">
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column label="订单编号" align="center" prop="id" width="190" />
      <el-table-column label="活动名称" align="center" prop="activeName" min-width="200" :show-overflow-tooltip="true" />
      <el-table-column label="订单类型" align="center" prop="orderTypeStr" />
      <el-table-column label="订单名称" align="center" prop="name" min-width="200" :show-overflow-tooltip="true" />
      <el-table-column label="订单金额" align="center" prop="total" />
      <el-table-column label="下单账号" align="center" prop="createBy" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="下单时间" align="center" prop="handleDate" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column label="付款时间" align="center" prop="payDate" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column label="付款方式" align="center" prop="paymentTypeStr" width="80" />
      <el-table-column label="交易流水号" align="center" prop="payId" min-width="180" :show-overflow-tooltip="true" />

      <el-table-column label="订单状态" align="center" prop="status" width="80">
        <template #default="scope">
          {{ statusArr[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column label="开票状态" align="center">
        <template #default="scope">
          {{ scope.row.isInvoice=='1'?'已开票':(scope.row.invoiceId==0?'未申请':'申请中') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="100" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
          >详情</el-button>
          <el-button
            type="text"
            @click="handleRemarks(scope.row)"
          >备注</el-button>
          <el-button
            v-if="scope.row.status=='0'"
            type="text"
            @click="handleCancel(scope.row)"
          >取消订单</el-button>
          <el-button
            v-if="scope.row.status=='7'"
            type="text"
            @click="handleBack(scope.row)"
          >确认退款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
    <order-hotel-info ref="orderHotelInfoRef" />
    <order-reception-info ref="orderReceptionInfoRef" />
    <order-dining-info ref="orderDiningInfoRef" />
    <modify-remarks ref="modifyRemarksRef" @submitForm="getList" />
  </div>
</template>

<script setup name="Logistics_Order">
import { listOrderNew, orderBack, orderCancel } from '@/api/logex/order'
import { getCurrentInstance, ref, toRefs } from 'vue'
import { onMounted, reactive } from '@vue/runtime-core'
import { useRoute } from 'vue-router'
import usePermissionStore from '@/store/modules/permission'
import OrderHotelInfo from './dialog/orderHotelInfo'
import OrderReceptionInfo from './dialog/orderReceptionInfo'
import OrderDiningInfo from './dialog/orderDiningInfo'
import ModifyRemarks from './dialog/modifyRemarks'


const { proxy } = getCurrentInstance()
const route = useRoute()
const permissionStore = usePermissionStore()

const orderList = ref([])
const loading = ref(true)
const total = ref(0)
const statusArr = ['付款中', '付款成功', '取消付款', '退订', '退订', '', '', '申请退款']
const isBelongMatch = ref(false)


const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: undefined,
    activeId: undefined,
    activeName: undefined,
    handTime: undefined,
    handTimeRange: undefined
  }
})

const { queryParams } = toRefs(data)

onMounted(() => {
  if (route.params.matchId) {
    permissionStore.setActiveMenu('/logistics/match')
    isBelongMatch.value = true
  }
})

/** 查询订单列表 */
function getList() {
  loading.value = true
  if (queryParams.value.handTime) {
    queryParams.value.handTimeRange = queryParams.value.handTime.join(',')
  } else {
    queryParams.value.handTimeRange = null
  }

  if (route.params.matchId) {
    queryParams.value.activeId = route.params.matchId
  }

  listOrderNew(queryParams.value).then(response => {
    orderList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

function handleView(row) {
  switch (row.orderType) {
    case 0:// 酒店订单
      proxy.$refs['orderHotelInfoRef'].open(row)
      break
    case 1:// 接送订单
      proxy.$refs['orderReceptionInfoRef'].open(row)
      break
    case 2:// 餐饮订单
      proxy.$refs['orderDiningInfoRef'].open(row)
      break
  }
}

function handleRemarks(row) {
  proxy.$refs['modifyRemarksRef'].open(row)
}

function handleCancel(row) {
  proxy.$modal.confirm('是否确认取消订单 "' + row.id + '" ?').then(function() {
    return orderCancel(row.id)
  }).then((res) => {
    proxy.$modal.msgSuccess(res.msg)
    getList()
  })
}

function handleBack(row) {
  proxy.$modal.confirm('是否确认退款，订单编号："' + row.id + '" ?').then(function() {
    return orderBack(row.id)
  }).then((res) => {
    proxy.$modal.msgSuccess(res.msg)
    getList()
  })
}

function handleExport() {
  if (queryParams.value.handTime) {
    queryParams.value.handTimeRange = queryParams.value.handTime.join(',')
  }

  proxy.download('logex/norder/exportOrder', {
    ...queryParams.value
  }, `订单信息_${new Date().getTime()}.xlsx`)
}

getList()
</script>
